function init() {
   clear();
   refreshSnacks();
}

function clear() {
   var form = $("addSnackForm");
   form.snackID.value = "";
   form.snackName.value = "";
   form.snackPrice.value = "";
   form.snackRemarks.value = "";
}

function refreshSnacks() {
   var ajax = new Ajax.Request(
      "../bin/getSnacks.php",
      {
         onSuccess: refreshSnacksOver
      }
   );
}

function refreshSnacksOver(req) {
   var div = $("snacksDiv");
   div.innerHTML = req.responseText;
}

function updateClicked(itemID, itemName, itemPrice, itemDesc) {
   var form = $("addSnackForm");
   form.snackID.value = itemID;
   form.snackName.value = itemName;
   form.snackPrice.value = itemPrice;
   form.snackRemarks.value = itemDesc;
}

function deleteClicked(itemID) {
   var ajax = new Ajax.Request(
      "../bin/updateSnack.php",
      {
         postBody: "op=delete&from=admin&snackID=" + itemID,
         onSuccess: updateSnacksOver
      }
   );
}

function updateXML() {
   var ajax = new Ajax.Request(
      "../bin/updateSnack.php",
      {
         postBody: "op=updateXML&from=admin",
         onSuccess: updateSnacksOver
      }
   );
}

function verifyAddSnack() {
   var form = $("addSnackForm");
   var op="";
   
   if(parseInt(form.snackID.value) > 0) {
      op = "update";
   }
   else {
      form.snackID.value = -1;
      op = "add";
   }
   
   form.op.value = op;
   form.from.value = "admin";

   return true;
}

function updateSnacksOver(req) {
   if(req.responseText == "Success") {
      refreshSnacks();
      clear();
   }
   else {
      alert("Error on update");
   }
}